import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.io.File;
import java.io.IOException;
import java.util.Set;

public class test2 {


    public static void  main(String[] args) {
//        PhantomJSDriver webDriver = null;
        ChromeDriver webDriver = null;
        System.setProperty("webdriver.chrome.driver","C:\\Python2.7\\chromedriver.exe");//设置PhantomJs访问路径
        File file = new File("C://bjjj/screenshot");
        if (!file.exists()) file.mkdirs();
        try{
//             webDriver = getPhantomJs();
        webDriver=new ChromeDriver();

//             #phantomjs 需要改动下
//            driver = webdriver.Firefox()
//            driver.get("http://login.taobao.com")
//#windows 用Keys.CONTROL 如同ctrl+t
//            driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't') //打开一个新TAB
//            driver.get('http://mm.taobao.com/')
//            driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 'w')   //关闭一个浏览器 TAB
//            driver.close()
            String url="http://www.baidu.com";
            webDriver.manage().window().maximize();
            webDriver.get(url);
//            Set<String> windowHandles = webDriver.getWindowHandles();
//            for (String s:windowHandles
//                    ) {
//                System.out.println(s);
//
//            }
//            Thread.sleep(3000);
//            webDriver.get("https://www.taobao.com/");  //同一个webDriver 多次打开url 只会有一个windowhandler
//            windowHandles = webDriver.getWindowHandles();
//            for (String s:windowHandles
//                    ) {
//                System.out.println(s);
//
//            }

            Thread.sleep(3000);
            webDriver.findElementByTagName("body").sendKeys(Keys.COMMAND + "t");
            ChromeDriver chromeDriver = new ChromeDriver();
            chromeDriver.get("https://www.taobao.com/");
            Thread.sleep(3000);
            File screenshotAs1 = webDriver.getScreenshotAs(OutputType.FILE);
            FileUtils.copyFile(screenshotAs1,new File(file,System.currentTimeMillis()+".png"));
            Set<String> windowHandles = webDriver.getWindowHandles();

            WebElement kw = webDriver.findElementById("kw");
//            WebDriverWait wait = new WebDriverWait(webDriver, 10);
//            WebElement kw = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("kw")));//开始打开网页，等待输入元素出现
            kw.sendKeys("张三他爸");
            File screenshotAs = webDriver.getScreenshotAs(OutputType.FILE);
            FileUtils.copyFile(screenshotAs,new File(file,System.currentTimeMillis()+".png"));
            System.out.println("ok1");
//            WebDriverWait waitsu = new WebDriverWait(webDriver, 15);
//            WebElement su = waitsu.until(ExpectedConditions.presenceOfElementLocated(By.id("su")));//开始打开网页，等待输入元素出现
            Thread.sleep(3000);
            WebElement su = webDriver.findElementById("su");
            su.click();
            Thread.sleep(3000);
             screenshotAs = webDriver.getScreenshotAs(OutputType.FILE);
            FileUtils.copyFile(screenshotAs,new File(file,System.currentTimeMillis()+".png"));
            System.out.println("ok2");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
        if (webDriver != null) {
//           webDriver.quit();
        }
}
//zsly.mmktq
}

    public static PhantomJSDriver getPhantomJs() {
            String osname = System.getProperties().getProperty("os.name");
            System.out.println(osname);
            if (osname.equals("Linux")) {//判断系统的环境win or Linux
            System.setProperty("phantomjs.binary.path", "/usr/bin/phantomjs");
            } else {
            System.setProperty("phantomjs.binary.path", "C:\\Users\\XiangDian\\Downloads\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");//设置PhantomJs访问路径
            }
            DesiredCapabilities desiredCapabilities = DesiredCapabilities.phantomjs();
            //设置参数
            desiredCapabilities.setCapability("phantomjs.page.settings.userAgent", "Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0");
            desiredCapabilities.setCapability("phantomjs.page.customHeaders.User-Agent", "Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:50.0) Gecko/20100101 　　Firefox/50.0");
            String [] phantomJsArgs = {"--ignore-ssl-errors=true","--ssl-protocol=tlsv1"};
            desiredCapabilities.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, phantomJsArgs);
            boolean isProxy = false;
            if (isProxy) {      //是否使用代理
//            org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
//            proxy.setProxyType(org.openqa.selenium.Proxy.ProxyType.MANUAL);
//            proxy.setAutodetect(false);
//            String proxyStr = "";
//            do {
//            proxyStr = ProxyUtil.getProxy();//自定义函数，返回代理ip及端口
//            } while (proxyStr.length() == 0);
//            proxy.setHttpProxy(proxyStr);
//            desiredCapabilities.setCapability(CapabilityType.PROXY, proxy);
         }
            return new PhantomJSDriver(desiredCapabilities);
        }
}
